package org.dbstar.scheduler.actions;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.dbstar.scheduler.beans.OperateType;
import org.dbstar.scheduler.control.JobSchedulerExecutor;
import org.dbstar.scheduler.identifier.Identifier;


/**
 * 改变Job状态
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-5
 */
@Results({ @Result(name = "jobs", location = "jobs", type = "redirectAction") })
public class JobChangeAction extends ActionSupport {
	private JobSchedulerExecutor jobSchedulerExecutor;

	private Identifier identifier;

	private String change;

	public void setJobSchedulerExecutor(JobSchedulerExecutor jobSchedulerExecutor) {
		this.jobSchedulerExecutor = jobSchedulerExecutor;
	}

	public void setIdentifier(Identifier identifier) {
		this.identifier = identifier;
	}

	public void setChange(String change) {
		this.change = change;
	}

	@Override
	public String execute() throws Exception {
		if (identifier != null) {
			if ("kill".equals(change)) {
				jobSchedulerExecutor.kill(identifier);
				operate(OperateType.job_kill, identifier);
			} else if ("cancel".equals(change)) {
				jobSchedulerExecutor.cancel(identifier);
				operate(OperateType.job_cancel, identifier);
			} else if ("pause".equals(change)) {
				jobSchedulerExecutor.pause(identifier);
				operate(OperateType.job_pause, identifier);
			} else if ("resume".equals(change)) {
				jobSchedulerExecutor.resume(identifier);
				operate(OperateType.job_resume, identifier);
			}
		}
		return "jobs";
	}
}
